#include "StdAfx.h"

UIManager* Singleton<UIManager>::m_instance = 0;

UIManager::UIManager()
{
	new DialogHandler();
	m_enabled = true;
	m_pressedElem = 0;
	new CHelp();
}

void UIManager::Init()
{
	new MainMenu();
	Chatbox* box = new Chatbox();
	new Hud;
	AddElement(box);
	AddElement(box);
	InputBox* ibox = new InputBox(10, 655, 400, 32);
	AddElement(ibox);
	new Castbar();
	new TooltipManager;	
}

void UIManager::Render()
{
	if(!m_enabled)
		return;

	sD3D->SetRenderState(D3DRS_ZENABLE, FALSE);
	sD3D->SetVertexShader(NULL);
	sD3D->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
	sD3D->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
	sD3D->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
	sD3D->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);

	std::set<UIElement*>::iterator itr = m_elements.begin();
	std::set<UIElement*>::iterator end = m_elements.end();

	for( ; itr != end; ++itr)
	{
		(*itr)->Draw();
	}

	DialogHandler::GetInstance()->Render();

	sMainMenu.Render();

	sHud.Draw();

	sHelp.Render();

	sTooltipMgr.Render();

	sD3D->SetRenderState(D3DRS_ZENABLE, TRUE);
	sD3D->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
}

void UIManager::OnMouseMove(Vertex2F pos)
{
	if(!m_enabled)
		return;

	//sBrowsing.OnMouseMove(pos);

	std::set<UIElement*>::iterator itr = m_elements.begin();
	std::set<UIElement*>::iterator end = m_elements.end();

	DialogHandler::GetInstance()->OnMouseMove((ui32)pos.x, (ui32)pos.y, false, false);

	for( ; itr != end; ++itr)
	{
		(*itr)->OnMouseMove((ui32)pos.x, (ui32)pos.y, false, false);
	}

	if(sD3D.GetAppState() == STATE_MENU) {
	}
}

bool UIManager::OnMouseActivity(Vertex2F pos, bool up)
{
	if(!m_enabled)
	{
		m_uiHold = false;
		return false;
	}

	/*if(sBrowsing.OnMouseClick(pos, up))
	{
		m_uiHold = up ? false : true;
		return true;
	}*/

	std::set<EditBox*>::iterator eitr = m_editBoxes.begin();
	std::set<EditBox*>::iterator eend = m_editBoxes.end();

	for( ; eitr != eend; ++eitr)
		(*eitr)->MouseActivity(true, pos, up);

	if(DialogHandler::GetInstance()->MouseActivity(true, pos, up))
	{
		m_uiHold = up ? false : true;
		return true;
	}

	if(InputBox::GetInstance())
		sInput.MouseActivity(false, pos, up);

	std::set<UIElement*>::reverse_iterator itr = m_elements.rbegin();
	std::set<UIElement*>::reverse_iterator end = m_elements.rend();

	for( ; itr != end; ++itr)
	{
		if(pos.x >= (*itr)->m_position.x && pos.x <= ((*itr)->m_position.x + (*itr)->m_size.x)
			&& pos.y >= (*itr)->m_position.y && pos.y <= ((*itr)->m_position.y + (*itr)->m_size.y)
			&& (*itr)->m_visible && !(*itr)->m_noHit)
		{
			if(up)
			{
				if(m_pressedElem)
				{
					m_pressedElem->OnMouseClick();
					m_pressedElem->MouseActivity(true, pos, up);
					m_pressedElem->m_state = 0;
				}
				m_pressedElem = 0;
				m_uiHold = false;
				return true;
			}
			else
			{
				if((*itr) == m_pressedElem) // would be weird, but who knows...
				{
					m_uiHold = true;
					return true;
				}
				if(m_pressedElem)
					m_pressedElem->m_state = 0;
				m_pressedElem = (*itr);
				m_pressedElem->MouseActivity(true, pos, false);
				m_pressedElem->m_state = 1;
				m_uiHold = true;
				return true;
			}
		}
	}
	bool mainMenu = false;
	if(up)
	{
		if(sMainMenu.OnMouseClick(pos))
			mainMenu = true;
	}
	if(m_pressedElem)
	{
		m_pressedElem->MouseActivity(true, pos, true);
		m_pressedElem->m_state = 0;
	}
	m_pressedElem = 0;
	m_uiHold = mainMenu && !up;
	return mainMenu;
}

void UIManager::OnChar(char c)
{
	if(!m_enabled)
		return;
	std::set<EditBox*>::iterator itr = m_editBoxes.begin();
	std::set<EditBox*>::iterator end = m_editBoxes.end();

	for( ; itr != end; ++itr)
		if((*itr)->m_visible)
			(*itr)->OnInput(c);
}

bool UIManager::GetEditFocus()
{
	if(!m_enabled)
		return false;
	std::set<EditBox*>::iterator itr = m_editBoxes.begin();
	std::set<EditBox*>::iterator end = m_editBoxes.end();

	for( ; itr != end; ++itr)
		if((*itr)->GetFocus())
			return true;
	return false;
}